package com.xinzhi.service.impl;

import com.xinzhi.dao.CartMapper;
import com.xinzhi.entity.Cart;
import com.xinzhi.entity.Result;
import com.xinzhi.entity.User;
import com.xinzhi.entity.vo.CartInfo;
import com.xinzhi.entity.vo.CartVo;
import com.xinzhi.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Override
    public Result addProduct(String productId, HttpServletRequest httpServletRequest) {
        //判空
        if (productId == null || productId ==""){
            return Result.error("productId参数为空");
        }
        //转换数据类型
        Integer tempProductId = Integer.valueOf(productId);
        //获取 session 中的用户信息 如果不存在说明未登录
        User user = (User) httpServletRequest.getSession().getAttribute("User");
        if (user.getUsername() == null || user.getUsername() == ""){
            return Result.error("用户未登录");
        }
        Cart tempCart = new Cart();
        tempCart.setId(0);
        tempCart.setNum(1);
        tempCart.setUserid(user.getId());
        tempCart.setProductid(tempProductId);
        //用户已登录  查看购物车中是否存在该商品
        Cart cart = cartMapper.queryProduct(tempProductId, user.getId());
        int i = 0;
        if (cart.getProductid() == 0){
            i = cartMapper.addProduct(cart);
        }else {
            i = cartMapper.plusProductNum(tempProductId,user.getId());
        }
        if (i != 0){
            return Result.ok("商品成功加入购物车");
        }
        return Result.error("系统错误，添加购物车失败");
    }

    @Override
    public Result checkCart(HttpServletRequest httpServletRequest) {
        // 判断用户是否登录
        User user = (User) httpServletRequest.getSession().getAttribute("User");
        if (user.getUsername() == null || user.getUsername() == ""){
            return Result.error("用户未登录");
        }
        List<CartInfo> cartInfos = cartMapper.checkCart(user.getId());
        System.out.println(cartInfos);
        BigDecimal countMonty = new BigDecimal("0");
        for (CartInfo cartInfo : cartInfos) {
            BigDecimal price = cartInfo.getProductIntro().getPrice();
            BigDecimal num = BigDecimal.valueOf(cartInfo.getNum());
            BigDecimal multiply = price.multiply(num);
            System.out.println(multiply);
            countMonty = countMonty.add(multiply);
        }
        CartVo cartVo = new CartVo();
        cartVo.setCartInfoList(cartInfos);
        cartVo.setCountMoney(countMonty);
        return Result.ok(cartVo);
    }

    @Override
    public Result plusProduct(String productId,HttpServletRequest httpServletRequest) {
        //判空
        if (productId == null || productId ==""){
            return Result.error("productId参数为空");
        }
        //转换参数类型
        Integer tempProductId = Integer.valueOf(productId);
        //添加商品说明用户已经登录
        User user = (User) httpServletRequest.getSession().getAttribute("User");

        int i = cartMapper.plusProductNum(tempProductId,user.getId());
        if (i != 0){
            return Result.ok("商品已成功加入购物车");
        }
        return Result.error("系统错误，请重新选择");
    }

    @Override
    public Result subtractProduct(String productId, HttpServletRequest httpServletRequest) {
        //判空
        if (productId == null || productId ==""){
            return Result.error("productId参数为空");
        }
        //转换参数类型
        Integer tempProductId = Integer.valueOf(productId);
        //添加商品说明用户已经登录
        User user = (User) httpServletRequest.getSession().getAttribute("User");
        int i = cartMapper.subtractProduct(tempProductId, user.getId());
        if (i != 0){
            return Result.ok("商品-1");
        }
        return Result.error("系统错误，请重新选择");
    }

    @Override
    public Result deleteProduct(String productId, HttpServletRequest httpServletRequest) {
        //判空
        if (productId == null || productId ==""){
            return Result.error("productId参数为空");
        }
        //转换参数类型
        Integer tempProductId = Integer.valueOf(productId);
        //添加商品说明用户已经登录
        User user = (User) httpServletRequest.getSession().getAttribute("User");
        int i = cartMapper.deleteProduct(tempProductId, user.getId());
        if (i != 0){
            return Result.ok("商品删除成功");
        }
        return Result.error("系统错误，请重新选择");
    }
}
